package enterprise.web.tookit.email;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.PostLoad;
import javax.persistence.PostPersist;
import javax.persistence.PostRemove;
import javax.persistence.PostUpdate;
import javax.persistence.PrePersist;
import javax.persistence.PreRemove;
import javax.persistence.PreUpdate;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlRootElement;

import org.apache.log4j.Logger;

/**
 * @since 01/09/2012
 * 
 * @author regisrocha
 * 
 * @description Classe para mapeamento da entidade enterprisewebtoolkit_db.config_email_webtoolkit
 * 
 */
@Entity(name = "email")
@Table(name = "config_email_webtoolkit")
@XmlRootElement
public class Email implements Serializable {

	/**
	 * serial version
	 */
	private static final long serialVersionUID = -4894180782540914322L;

	/**
	 * LOG
	 */
	private static Logger log = Logger.getLogger(Email.class);

	private Integer id;

	private boolean active;

	private String hostName;

	private Integer smtpPort;

	private String emailFrom;

	private String usernameEmail;

	private String passwordEmail;

	private boolean sslOnConnect;

	/**
	 * @return the id
	 */

	@Id
	@Column(name = "id", nullable = false)
	public Integer getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 * @return the hostName
	 */
	@Column(name = "host_name", nullable = false, length = 255)
	public String getHostName() {
		return hostName;
	}

	/**
	 * @param hostName
	 *            the hostName to set
	 */
	public void setHostName(String hostName) {
		this.hostName = hostName;
	}

	/**
	 * @return the smtpPort
	 */
	@Column(name = "smtp_port", nullable = false)
	public Integer getSmtpPort() {
		return smtpPort;
	}

	/**
	 * @param smtpPort
	 *            the smtpPort to set
	 */
	public void setSmtpPort(Integer smtpPort) {
		this.smtpPort = smtpPort;
	}

	/**
	 * @return the emailFrom
	 */
	@Column(name = "email_from", nullable = false, length = 100)
	public String getEmailFrom() {
		return emailFrom;
	}

	/**
	 * @param emailFrom
	 *            the emailFrom to set
	 */
	public void setEmailFrom(String emailFrom) {
		this.emailFrom = emailFrom;
	}

	/**
	 * @return the usernameEmail
	 */
	@Column(name = "username_email", nullable = false, length = 100)
	public String getUsernameEmail() {
		return usernameEmail;
	}

	/**
	 * @param usernameEmail
	 *            the usernameEmail to set
	 */
	public void setUsernameEmail(String usernameEmail) {
		this.usernameEmail = usernameEmail;
	}

	/**
	 * @return the passwordEmail
	 */
	@Column(name = "password_email", nullable = false, length = 100, updatable = false)
	public String getPasswordEmail() {
		return passwordEmail;
	}

	/**
	 * @param passwordEmail
	 *            the passwordEmail to set
	 */
	public void setPasswordEmail(String passwordEmail) {
		this.passwordEmail = passwordEmail;
	}

	/**
	 * @return the sslOnConnect
	 */
	@Column(name = "sslOnConnect", nullable = false)
	public boolean isSslOnConnect() {
		return sslOnConnect;
	}

	/**
	 * @param sslOnConnect
	 *            the sslOnConnect to set
	 */
	public void setSslOnConnect(boolean sslOnConnect) {
		this.sslOnConnect = sslOnConnect;
	}

	/**
	 * @return the active
	 */
	@Column(name = "active", nullable = false)
	public boolean isActive() {
		return active;
	}

	/**
	 * @param active
	 *            the active to set
	 */
	public void setActive(boolean active) {
		this.active = active;
	}

	@Override
	public String toString() {
		final StringBuilder toString = new StringBuilder();

		toString.append(" ID: " + this.id);
		toString.append(" ACTIVE: " + this.active);
		toString.append(" FROM: " + this.emailFrom);
		toString.append(" HOSTNAME: " + this.hostName);
		toString.append(" SMTP_PORT: " + this.smtpPort);
		toString.append(" EMAIL: " + this.usernameEmail);
		toString.append(" SSL_ON_CONNECT: " + this.sslOnConnect);

		return toString.toString();
	}

	@PostLoad
	public void postLoad() {
		printInfoLog("Loading email: " + this.toString());
	}

	@PrePersist
	public void prePersist() {
		printInfoLog("Persisting email: " + this.toString());
	}

	@PostPersist
	public void postPersist() {
		printInfoLog("Email persisted, ID: " + this.id);
	}

	@PreUpdate
	public void preUpdate() {
		printInfoLog("Pre updating email: " + this.toString());
	}

	@PostUpdate
	public void postUpdate() {
		printInfoLog("Email updated, iD: " + this.id);
	}

	@PreRemove
	public void preRemove() {
		printInfoLog("Pre removing email: " + this.toString());
	}

	@PostRemove
	public void postRemove() {
		printInfoLog("Email Removed, id: " + this.id);
	}

	private final void printInfoLog(final String messageInfoLog) {
		if (log == null) {
			log = Logger.getLogger(Email.class);
		}

		log.info(messageInfoLog);
	}
}